package com.yh.autobio.modules.adatper;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yh.autobio.R;
import com.yh.autobio.common.PLog;
import com.yh.autobio.modules.bean.WorkInfo;

import java.util.ArrayList;

/**
 * Created by imyar on 2016/5/3 0031.
 */
public class RKAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private static String TAG = RKAdapter.class.getSimpleName();

    private Context mContext;
    private ArrayList<WorkInfo> workInfos;

    public ArrayList<WorkInfo> getWorkInfos() {
        return workInfos;
    }

    public RKAdapter(Context context, ArrayList<WorkInfo> workInfos) {
        mContext = context;
        this.workInfos = workInfos;
    }

    @Override public int getItemViewType(int position) {
        return super.getItemViewType(position);
    }

    @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return new WorkEntryViewHolder(
                    LayoutInflater.from(mContext).inflate(R.layout.item_hour_info, parent, false));
    }

    @Override public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

        if (holder instanceof WorkEntryViewHolder) {
            try {
                for (int i = 0; i < workInfos.size(); i++) {
                    //s.subString(s.length-3,s.length);
                    //第一个参数是开始截取的位置，第二个是结束位置。
                    final WorkInfo mDate = workInfos.get(i);
                    ((WorkEntryViewHolder) holder).mClock[i].setText(
                            mDate.getFddno());
                    ((WorkEntryViewHolder) holder).mTemp[i].setText(mDate.getFBillNo() + "");
                    ((WorkEntryViewHolder) holder).mHumidity[i].setText(
                            mDate.getCount() + "");
                    ((WorkEntryViewHolder) holder).mWind[i].setChecked(true);
                    mDate.setFwgtype(true);
                    ((WorkEntryViewHolder) holder).mWind[i].setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                        @Override
                        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                            mDate.setFwgtype(isChecked);
                        }
                    });
                }
            } catch (Exception e) {
                //Snackbar.make(holder.itemView, R.string.api_error, Snackbar.LENGTH_SHORT).show();
                PLog.e(TAG, e.toString());
            }
        }
    }

    @Override public int getItemCount() {
        return 1;
    }

    /**
     * 工序详情
     */
    class WorkEntryViewHolder extends RecyclerView.ViewHolder {
        private LinearLayout itemHourInfoLinearlayout;
        private TextView[] mClock = new TextView[workInfos.size()];
        private TextView[] mTemp = new TextView[workInfos.size()];
        private TextView[] mHumidity = new TextView[workInfos.size()];
        private CheckBox[] mWind = new CheckBox[workInfos.size()];


        public WorkEntryViewHolder(View itemView) {
            super(itemView);
            itemHourInfoLinearlayout = (LinearLayout) itemView.findViewById(R.id.item_hour_info_linearlayout);

            for (int i = 0; i < workInfos.size(); i++) {
                View view = View.inflate(mContext, R.layout.item_rk_line, null);
                mClock[i] = (TextView) view.findViewById(R.id.one_clock);
                mTemp[i] = (TextView) view.findViewById(R.id.one_temp);
                mHumidity[i] = (TextView) view.findViewById(R.id.one_humidity);
                mWind[i] = (CheckBox) view.findViewById(R.id.one_wind);
                itemHourInfoLinearlayout.addView(view);
            }
        }
    }
}
